package servlet.hou;

import bean.Result;
import com.fasterxml.jackson.databind.ObjectMapper;
import dao.UserDaoImpl;
import utils.JwtUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;


//宿管更新
@WebServlet("/hou/updatePublicByHou")
public class UpdatePublicByHouServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Integer publicId = Integer.valueOf(request.getParameter("publicId"));
        String token = request.getHeader("user_token_header");
        String publicName = request.getParameter("publicName");
        Integer buildId = JwtUtils.getHouBuildIdFromToken(token,"buildId");
        if (request.getParameter("buildId") != null) {
            buildId = Integer.valueOf(request.getParameter("buildId"));

        }
        Integer publicCount = null;
        if (request.getParameter("publicCount") != null) {
            publicCount = Integer.valueOf(request.getParameter("publicCount"));

        }
        Integer publicAlready = null;
        if (request.getParameter("publicAlready") != null) {
            publicAlready = Integer.valueOf(request.getParameter("publicAlready"));

        }
        String stuName = request.getParameter("stuName");
        Integer stuId = null;
        if (request.getParameter("stuId") != null) {
            stuId = Integer.valueOf(request.getParameter("stuId"));

        }
        Integer publicTime = null;
        if (request.getParameter("publicTime") != null) {
            publicTime = Integer.valueOf(request.getParameter("publicTime"));

        }
        Integer deleteFlag = null;
        if (request.getParameter("deleteFlag") != null) {
            deleteFlag = Integer.valueOf(request.getParameter("deleteFlag"));

        }
        Integer data= null;
        try {
            data = UserDaoImpl.updatePublicByHou(publicId, publicName, buildId, publicCount, publicAlready, stuName, stuId, publicTime, deleteFlag);
        } catch (SQLException e) {
            System.out.println(e);
        }
        Result result = new Result();
        if(data!=null){
//
            result.success(data);
            ObjectMapper objectMapper = new ObjectMapper();
            String str = objectMapper.writeValueAsString(result);
            response.setContentType("application/json;charset=utf8");
            response.getWriter().print(str);


        }else{
//
            result.fail();
            ObjectMapper objectMapper = new ObjectMapper();
            String str = objectMapper.writeValueAsString(result);
            response.setContentType("application/json;charset=utf8");
            response.getWriter().print(str);


        }
    }
}
